package com.yungu.swift.api.route.v1.common.h5;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.config.ApplicationConfig;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.system.sys.model.vo.BusinessVo;
import com.yungu.swift.system.sys.service.SysBusinessOrganizationService;
import com.yungu.swift.utils.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

import static com.yungu.swift.api.config.NacosListener.SYS_CONFIG;

@Controller
@RequestMapping(value = "/h5/agreement")
public class AgreementController {

    @Reference
    private SysBusinessOrganizationService sysBusinessOrganizationService;

    @RequestMapping(value = {"/{type}"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String agreement(@RequestParam String appid, @PathVariable String type) {
        return appid + "/agreement/" + type;
    }

    @RequestMapping(value = {"/common/{type}"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String agreementCom(@RequestParam String appid, @PathVariable String type) {
        if (appid.equals(SYS_CONFIG.getAppid())) {
            return "commonPage/agreement/" + type;
        }
        return null;
    }

    @RequestMapping(value = "/common/trip/share", method = {RequestMethod.POST, RequestMethod.GET})
    public String tripShare(@RequestParam String appid) {
        if (appid.equals(SYS_CONFIG.getAppid())) {
            return "commonPage/tripShare";
        }
        return null;
    }


    @RequestMapping(value = {"/{appid}/select/business"}, method = {RequestMethod.POST, RequestMethod.GET})
    public String agreement(@PathVariable String appid, Model model) {
        if (appid.equals(SYS_CONFIG.getAppid())) {
            ResponseData<List<BusinessVo>> getBusinessRsd = sysBusinessOrganizationService.getBusiness(MapUtils.build("organizationUuid", SYS_CONFIG.getAppid()));
            List<BusinessVo> businessVoList = getBusinessRsd.getData();
            if(CollectionUtils.isEmpty(businessVoList)){
                return null;
            }
            StringBuilder openBusiness = new StringBuilder();
            List<String> configOpenList = ApplicationConfig.DRIVER_CONFIG.getLogin().getDriverRegisterType();
            for(BusinessVo businessVo : businessVoList){
                if(configOpenList.contains(businessVo.getType().toString())){
                    openBusiness.append(",").append(businessVo.getType());
                }
            }
            model.addAttribute("openBusiness",openBusiness.substring(1,openBusiness.length()));
            return "commonPage/selectBusiness";
        }
        return null;
    }
}
